package com.example.bilibili_miaosha.vo;

/**
 * @ClassName IsMobileValidator
 * @Description 手机号校验规则 ，这个是用来配合IsMobile实现注解功能的
 * @Author 川
 * @Date 2022/3/28 21:03
 * @Version 1.0
 **/
import com.example.bilibili_miaosha.utils.IsMobile;
import com.example.bilibili_miaosha.utils.ValidatorUtil;
import org.springframework.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class IsMobileValidator implements ConstraintValidator<IsMobile,String> {

    private boolean required = false;

    @Override
    public void initialize(IsMobile constraintAnnotation) {
        required = constraintAnnotation.required();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (required){
            return ValidatorUtil.isMobile(value);
        }else {
            if (StringUtils.isEmpty(value)){
                return true;
            }else {
                return ValidatorUtil.isMobile(value);
            }
        }
    }
}
